package com.woniuxy.controller;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.woniuxy.config.AlipayConfig;
import com.woniuxy.pojo.ServerSubscribe;
import com.woniuxy.service.AlipayService;
import com.woniuxy.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

/**
 * 支付接口
 */


@Controller
@RequestMapping("/pay")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
public class AliPayController {

    @Autowired
    private ServerService serverService;

    @Autowired
    private AlipayService alipayService;





    @RequestMapping("/pay")
    @ResponseBody
    public void payMent(HttpServletResponse response, HttpServletRequest request,String price) {
        Map<String,Object> map = new HashMap<>();
        try {
            alipayService.aliPay(response,request,price);
            map.put("code",200);
            map.put("msg","success");
        } catch (IOException e) {
            e.printStackTrace();
            map.put("code",200);
            map.put("msg","success");
        }
        //return map;
    }

    //支付完成后的异步通知，讲数据插入预约表中
    @RequestMapping("/notifyUrl")
    public void notify(HttpServletRequest request,HttpServletResponse response)throws Exception {
        System.out.println("异步方法执行。。。");
//        ServerSubscribe serverSubscribe = (ServerSubscribe) request.getSession().getAttribute("userid");
//        System.out.println("获取的数据为："+serverSubscribe);
//        //插入预约表中
//        serverService.insertServerSubscribe(serverSubscribe);
    }

    @RequestMapping("/returnUrl")
    public void returnUrl(HttpServletRequest request,HttpServletResponse response)throws Exception {
        System.out.println("同步方法执行。。。");
//        ServerSubscribe serverSubscribe = (ServerSubscribe) request.getSession().getAttribute("userid");
//        System.out.println("获取的数据为："+serverSubscribe);
//        //插入预约表中
//        serverService.insertServerSubscribe(serverSubscribe);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        //return map;
    }


}
